import { createRouter, createWebHashHistory } from "vue-router";

// 路由
const routes = [
  // 定向首页
  {
    path: "/",
    redirect: "/index",
  },
  // 首页
  {
    path: "/index",
    component: () => import("../views/home/Index"),
    children: [
      {
        path: "/index",
        redirect: "/index/content",
      },
      // 首页
      {
        path: "/index/content",
        component: () => import("../views/home/content/Content"),
        meta: {
          requireLogin: true,
        },
      },
      // 产品管理
      {
        path: "/index/manage",
        component: () => import("../views/home/manage/Manage"),
        meta: {
          requireLogin: true,
        },
        children: [
          {
            path: "/index/manage",
            redirect: "/index/manage/list",
          },
          {
            //产品列表
            path: "/index/manage/list",
            component: () => import("../views/home/manage/component/List.vue"),
          },
          {
            //产品状态
            path: "/index/manage/status",
            component: () =>
              import("../views/home/manage/component/Status.vue"),
          },
          {
            //产品发布
            path: "/index/manage/release",
            component: () =>
              import("../views/home/manage/component/Release.vue"),
          },
        ],
      },
      // 店铺分布
      {
        path: "/index/distribute",
        component: () => import("../views/home/distribute/Distribute"),
        meta: {
          requireLogin: true,
        },
      },
      // 留言
      {
        path: "/index/leave",
        component: () => import("../views/home/leave/Leave"),
        meta: {
          requireLogin: true,
        },
      },
      // 管理员
      {
        path: "/index/admin",
        component: () => import("../views/home/admin/Admin"),
        meta: {
          requireLogin: true,
        },
        children: [
          {
            path: "/index/admin",
            redirect: "/index/admin/list",
          },
          {
            path: "/index/admin/list",
            component: () =>
              import("../views/home/admin/components/AdminList.vue"),
          },
          {
            path: "/index/admin/manage",
            component: () =>
              import("../views/home/admin/components/Manage.vue"),
          },
        ],
      }, // 账号设置
      {
        path: "/index/account",
        component: () => import("../views/home/account/Account"),
        meta: {
          requireLogin: true,
        },
        children: [
          {
            path: "/index/account",
            redirect: "/index/account/baseAccount",
          },
          {
            path: "/index/account/baseAccount",
            component: () => import("../views/home/account/baseAccount"),
          },
          {
            path: "/index/account/userAccount",
            component: () => import("../views/home/account/userAccount"),
          },
        ],
      },
    ],
  },
  // 登录注册
  {
    path: "/login",
    component: () => import("../views/login/Login"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
//所有页面需登录才能进入，sessionStorage.getItem("token")
router.beforeEach((to, from, next) => {
  if (to.meta.requireLogin) {
    if (sessionStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});
export default router;
